<template>
 <div>
   <!-- element表单插件的使用  :model   是表单数据对象   :rules   是表单验证规则-->
   <el-form 
      :model="ruleForm" 
      :rules="rules" 
      ref="ruleForm" 
      label-width="0px" 
      class="demo-ruleForm login-container">
      <h3 class="title">后台登录系统</h3>
      <!-- prop  是校验规则  在rules中寻找 -->
      
      <el-form-item  prop="username" >
          <el-input prefix-icon="icon iconfont icon-login_user" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item  prop="password" >
          <el-input prefix-icon="icon iconfont icon-login_password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class='remember'>记住密码</el-checkbox>
      <el-button 
      @click='handleClickBtn()' 
      :loading="isLogin"
      style='width:100%' 
      type="primary" >登录</el-button>
   </el-form>
 </div>
</template>

<script>
export default {
  name: 'login',
  data(){
    return {
      ruleForm:{
        username:'',
        password:''
      },
      rules:{
        username:[
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
        ],
        password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      },
      checked:true,
      isLogin:false
    }
  },
  methods: {
    handleClickBtn(){
      //校验的方法    validate
      // console.log(12)
       this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.isLogin = true
            this.$axios.post('login',this.ruleForm).then( res => { 
              this.isLogin = false
              // console.log(res)
              if(res.data.meta.status==400){             
                this.$message({
                    showClose: true,
                    message: res.data.meta.msg,
                    type: 'error'
                })                  
              }else{
                  this.$message({
                  showClose: true,
                  message: res.data.meta.msg,
                  type: 'success'
                })
                localStorage.setItem('myToken',res.data.data.token)
                this.$router.push({path:'/layout/users'})
              }
            })            
          } else {
            return false;
          }
       })
    }, 
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
@import '../../assets/iconfont/iconfont.css';
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 100px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  
}
.title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
}
.remember {
    margin: 0px 0px 35px 0px;
}
</style>
